<template>
  <van-skeleton :row="20" :loading="!apartmentDetailInfo?.id">
    <div class="page-container">
      <!--  轮播图-->
      <div class="hero-swipe-container">
        <van-swipe 
          class="hero-swipe" 
          :autoplay="4000" 
          indicator-color="rgba(255,255,255,0.8)"
          :show-indicators="apartmentDetailInfo.graphVoList?.length > 1"
        >
          <van-swipe-item
            v-for="item in apartmentDetailInfo.graphVoList"
            :key="item.url"
            class="swipe-item"
          >
            <van-image 
              fit="cover" 
              :src="item.url" 
              width="100vw" 
              height="45vh"
              class="hero-image"
            >
              <template v-slot:error>
                <div class="image-error">
                  <van-icon name="photo-fail" size="32" color="#ccc" />
                  <span>图片加载失败</span>
                </div>
              </template>
              <template v-slot:loading>
                <div class="image-loading">
                  <van-loading type="spinner" size="24" color="#277941" />
                </div>
              </template>
            </van-image>
            <!-- 渐变遮罩 -->
            <div class="hero-overlay"></div>
          </van-swipe-item>
        </van-swipe>
        
        <!-- 图片数量指示器 -->
        <div class="image-count" v-if="apartmentDetailInfo.graphVoList?.length > 1">
          <van-icon name="photo-o" size="14" />
          <span>{{ apartmentDetailInfo.graphVoList?.length }}</span>
        </div>
      </div>
      <!--  公寓的信息-->
      <div class="apartment-info-card">
        <div class="apartment-header">
          <div class="apartment-title-section">
            <h1 class="apartment-title">{{ apartmentDetailInfo.name }}</h1>
            <div class="apartment-tags" v-if="apartmentDetailInfo.labelInfoList?.length">
              <van-tag
                v-for="item in apartmentDetailInfo.labelInfoList"
                :key="item.id"
                type="primary"
                plain
                round
                size="small"
                class="apartment-tag"
              >
                {{ item.name }}
              </van-tag>
            </div>
          </div>
          
          <div class="apartment-price-section">
            <div class="price-container">
              <span class="price-symbol">￥</span>
              <span class="price-amount">{{ apartmentDetailInfo.minRent }}</span>
              <span class="price-unit">/月起</span>
            </div>
            <div class="price-label">起租价格</div>
          </div>
        </div>
      </div>
      <!--    基本信息-->
      <div class="info-section-card">
        <div class="section-header">
          <van-icon name="info-o" size="18" color="#277941" />
          <span class="section-title">社区介绍</span>
        </div>
        <div class="section-content">
          <p class="introduction-text">
            {{ apartmentDetailInfo.introduction }}
          </p>
        </div>
      </div>
      
      <!--    配套说明-->
      <div class="facilities-card">
        <div class="section-header">
          <van-icon name="setting-o" size="18" color="#277941" />
          <span class="section-title">配套设施</span>
        </div>
        <div class="facilities-grid">
          <div
            class="facility-item"
            v-for="item in apartmentDetailInfo.facilityInfoList"
            :key="item.id"
          >
            <div class="facility-icon">
              <SvgIcon :name="item.icon" size="28" />
            </div>
            <span class="facility-name">{{ item.name }}</span>
          </div>
        </div>
      </div>
      <!--    位置详情-->
      <div class="location-card">
        <div class="section-header">
          <van-icon name="location-o" size="18" color="#277941" />
          <span class="section-title">位置详情</span>
        </div>
        <div class="location-content">
          <div class="address-info">
            <van-icon name="guide-o" size="16" color="#666" />
            <span class="address-text">{{ apartmentDetailInfo.addressDetail }}</span>
          </div>
          <!--        地图容器-->
          <div class="map-container">
            <div id="container" class="map-view"></div>
          </div>
        </div>
      </div>
      <!--    可选房间列表-->
      <div class="rooms-section">
        <div class="section-header">
          <van-icon name="home-o" size="18" color="#277941" />
          <span class="section-title">可选房间</span>
          <div class="room-count" v-if="roomList?.length">
            <span>{{ roomList.length }}间</span>
          </div>
        </div>
        <div class="rooms-container">
          <PullDownRefreshContainer
            :request="getRoomListHandler"
            ref="pullDownRefreshContainerRef"
            class="rooms-list"
          >
            <template v-if="roomList?.length">
              <div class="room-cards-grid">
                <RoomCard
                  v-for="item in roomList"
                  :key="item.id"
                  :data="item"
                  class="room-card-item"
                ></RoomCard>
              </div>
            </template>
            <template v-else>
              <div class="empty-rooms">
                <van-icon name="home-o" size="48" color="#ccc" />
                <p>暂无可选房间</p>
              </div>
            </template>
          </PullDownRefreshContainer>
        </div>
      </div>
      
      <!--    预约看房-->
      <van-sticky :offset-bottom="0" position="bottom">
        <div class="appointment-button-container">
          <van-button 
            type="primary" 
            block 
            round
            size="large"
            class="appointment-button"
            @click="appointmentToViewHandle"
          >
            <van-icon name="calendar-o" size="18" />
            <span>预约看房</span>
          </van-button>
        </div>
      </van-sticky>
    </div>
  </van-skeleton>
</template>
<script setup lang="ts">
import { getApartmentDetailById, getRoomListByApartmentId } from "@/api/search";
import { onMounted, ref } from "vue";
import type { ApartmentInterface } from "@/api/search/types";
import { useMap } from "@/hooks/useMap";
import poiMarkerRed from "@/assets/poi-marker-red.png";
import RoomCard from "@/components/RoomCard/RoomCard.vue";
import PullDownRefreshContainer from "@/components/PullDownRefreshContainer/PullDownRefreshContainer.vue";
import type { ReqPage } from "@/api/types";
import type { RoomInterface } from "@/api/search/types";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();
// 公寓的详情信息
const apartmentDetailInfo = ref<ApartmentInterface>({} as ApartmentInterface);
// 获取公寓的详情信息
const getApartmentDetailHandle = async () => {
  const { data } = await getApartmentDetailById(route.query.id as string);
  apartmentDetailInfo.value = data;
};
// 房间列表
const roomList = ref<RoomInterface[]>([]);
const pullDownRefreshContainerRef =
  ref<InstanceType<typeof PullDownRefreshContainer>>();
// 分页获取房间的信息
async function getRoomListHandler(pageInfo: ReqPage) {
  //   调用接口
  const { data } = await getRoomListByApartmentId({
    id: route.query.id as string,
    ...pageInfo
  });
  data.records.forEach(item => {
    item.apartmentInfo = apartmentDetailInfo.value;
  });
  console.log("pageInfo.current", pageInfo.current);
  if (pageInfo.current === 1) {
    roomList.value = data.records;
  } else {
    roomList.value = [...roomList.value, ...data.records];
  }
  pullDownRefreshContainerRef.value?.setFinished(
    roomList.value.length >= data.total
  );
}
//#region <高德地图相关>
// 地图实例
const { AMap, initMap } = useMap();
function initMapPage(opts?: { lng: number; lat: number }) {
  console.log("opts", opts);
  const map = new AMap.value.Map("container", {
    zoom: 19, //初始地图级别
    center: [opts?.lng, opts?.lat], //初始地图中心点
    showIndoorMap: false //关闭室内地图
  });
  const icon = new AMap.value.Icon({
    size: new AMap.value.Size(25, 34), // 图标尺寸
    image: poiMarkerRed, // Icon的图像
    imageSize: new AMap.value.Size(25, 34)
  });
  const marker = new AMap.value.Marker({
    icon: icon,
    position: [opts?.lng, opts?.lat],
    anchor: "bottom-center"
  });
  map.add(marker);
  map.setFitView();
}
// 预约看房
const appointmentToViewHandle = () => {
  console.log("appointmentToViewHandle");
  router.push({
    path: "/appointment",
    query: { apartmentId: apartmentDetailInfo.value.id }
  });
};
//#endregion
onMounted(async () => {
  await getApartmentDetailHandle();
  console.log(apartmentDetailInfo.value.longitude);
  await initMap();
  initMapPage({
    lng: +apartmentDetailInfo.value.longitude,
    lat: +apartmentDetailInfo.value.latitude
  });
});
</script>

<style scoped lang="less">
/* 页面容器 */
.page-container {
  background: linear-gradient(180deg, #f8fffe 0%, var(--color-block-background) 100%);
  min-height: 100vh;
  padding-bottom: 80px;
}

/* 轮播图区域 */
.hero-swipe-container {
  position: relative;
  margin-bottom: 16px;
}

.hero-swipe {
  border-radius: 0 0 24px 24px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(39, 121, 65, 0.15);
}

.swipe-item {
  position: relative;
}

.hero-image {
  border-radius: 0;
}

.hero-image :deep(.van-image__img) {
  transition: transform 0.6s ease;
}

.hero-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    180deg,
    transparent 0%,
    rgba(0, 0, 0, 0.1) 70%,
    rgba(0, 0, 0, 0.3) 100%
  );
  pointer-events: none;
}

.image-count {
  position: absolute;
  top: 16px;
  right: 16px;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  color: white;
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.image-error,
.image-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 100%;
  background: #f5f5f5;
  color: #999;
}

/* 公寓信息卡片 */
.apartment-info-card {
  background: linear-gradient(135deg, #ffffff 0%, #f8fffe 100%);
  border-radius: 20px;
  padding: 20px;
  margin: 16px;
  box-shadow: 0 4px 24px rgba(39, 121, 65, 0.08);
  border: 1px solid rgba(39, 121, 65, 0.1);
  position: relative;
  overflow: hidden;
}

.apartment-info-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #277941 0%, #4CAF50 100%);
}

.apartment-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
}

.apartment-title-section {
  flex: 1;
}

.apartment-title {
  font-size: 24px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0 0 12px 0;
  line-height: 1.3;
}

.apartment-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.apartment-tag {
  background: rgba(39, 121, 65, 0.08) !important;
  border-color: rgba(39, 121, 65, 0.2) !important;
  color: #277941 !important;
  font-size: 12px;
  padding: 4px 12px;
  height: 28px;
  line-height: 20px;
}

.apartment-price-section {
  text-align: right;
  flex-shrink: 0;
}

.price-container {
  display: flex;
  align-items: baseline;
  justify-content: flex-end;
  color: #277941;
  font-weight: 700;
  margin-bottom: 4px;
}

.price-symbol {
  font-size: 16px;
  margin-right: 2px;
}

.price-amount {
  font-size: 28px;
  font-weight: 800;
}

.price-unit {
  font-size: 14px;
  color: #666;
  margin-left: 4px;
}

.price-label {
  font-size: 12px;
  color: #999;
}

/* 通用卡片样式 */
.info-section-card,
.facilities-card,
.location-card,
.rooms-section {
  background: white;
  border-radius: 16px;
  margin: 16px;
  padding: 20px;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

.info-section-card:hover,
.facilities-card:hover,
.location-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
}

/* 区域标题 */
.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 2px solid #f0f9ff;
  position: relative;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  flex: 1;
}

.room-count {
  background: rgba(39, 121, 65, 0.1);
  color: #277941;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

/* 社区介绍 */
.section-content {
  padding: 0;
}

.introduction-text {
  font-size: 15px;
  line-height: 1.6;
  color: #333;
  margin: 0;
  text-align: justify;
}

/* 配套设施 */
.facilities-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
  gap: 16px;
  margin-top: 8px;
}

.facility-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 8px;
  background: linear-gradient(135deg, #f8fffe 0%, #f0f9ff 100%);
  border-radius: 12px;
  transition: all 0.3s ease;
  border: 1px solid rgba(39, 121, 65, 0.1);
}

.facility-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(39, 121, 65, 0.15);
  background: linear-gradient(135deg, #ffffff 0%, #f8fffe 100%);
}

.facility-icon {
  margin-bottom: 8px;
  color: #277941;
  transition: transform 0.3s ease;
}

.facility-item:hover .facility-icon {
  transform: scale(1.1);
}

.facility-name {
  font-size: 12px;
  color: #333;
  text-align: center;
  font-weight: 500;
}

/* 位置详情 */
.location-content {
  padding: 0;
}

.address-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding: 12px 16px;
  background: #f8fffe;
  border-radius: 12px;
  border-left: 4px solid #277941;
}

.address-text {
  font-size: 14px;
  color: #333;
  line-height: 1.5;
}

.map-container {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border: 1px solid #e0e0e0;
}

.map-view {
  width: 100%;
  height: 240px;
}

/* 房间列表 */
.rooms-container {
  margin-top: 8px;
}

.rooms-list {
  min-height: 200px;
}

.room-cards-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.room-card-item {
  transition: all 0.3s ease;
}

.room-card-item:hover {
  transform: translateY(-2px);
}

.empty-rooms {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #999;
}

.empty-rooms p {
  margin: 12px 0 0 0;
  font-size: 14px;
}

/* 预约按钮 */
.appointment-button-container {
  padding: 16px;
  background: linear-gradient(180deg, transparent 0%, rgba(255, 255, 255, 0.95) 20%, white 100%);
  backdrop-filter: blur(8px);
}

.appointment-button {
  background: linear-gradient(135deg, #277941 0%, #4CAF50 100%) !important;
  border: none !important;
  box-shadow: 0 4px 20px rgba(39, 121, 65, 0.3) !important;
  height: 52px !important;
  font-size: 16px !important;
  font-weight: 600 !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 8px !important;
  transition: all 0.3s ease !important;
}

.appointment-button:active {
  transform: translateY(1px) !important;
  box-shadow: 0 2px 12px rgba(39, 121, 65, 0.4) !important;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .apartment-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  
  .apartment-price-section {
    text-align: left;
    width: 100%;
  }
  
  .price-container {
    justify-content: flex-start;
  }
  
  .facilities-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
  }
  
  .facility-item {
    padding: 12px 4px;
  }
  
  .facility-name {
    font-size: 11px;
  }
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.apartment-info-card,
.info-section-card,
.facilities-card,
.location-card,
.rooms-section {
  animation: fadeInUp 0.6s ease-out;
}

.apartment-info-card {
  animation-delay: 0.1s;
}

.info-section-card {
  animation-delay: 0.2s;
}

.facilities-card {
  animation-delay: 0.3s;
}

.location-card {
  animation-delay: 0.4s;
}

.rooms-section {
  animation-delay: 0.5s;
}
</style>
